var contextPath = $('input[name=contextPath]').val();
var sidx = "levelorder";
var sord = "asc";
var curr = 1;
var rows = 15;
var layer;
var serializeObject;
var form;
var state = 1;
var shopurl;
$(function() {
	
	shopurl = $("#shopurl").val();
	// 清除两边的空格 
	String.prototype.trim = function() { 
		return this.replace(/(^\s*)|(\s*$)/g, ''); 
	};

	contextPath = $('input[name=contextPath]').val();
	

});

layui.use('layer', function(){
	layer = layui.layer;

});




layui.use('form', function() {
	var $ = layui.jquery;
	form = layui.form();
	form.verify({
		number: function(value){
			if(value.trim().length < 1){
				return '请填写领域编号！';
			}else if(value.trim().length > 100){
				return '编号长度不可超过100！';
			}else{
				return checknone("number='"+value+"'");
			}
			
		},
		projectarea: function(value){
			//alert(value.length > 0 && checkAddName1(1)==1);
			if(value.trim().length < 1){
				return '请填写项目领域名称！';
			}else if(value.trim().length >100){
				return '项目领域名称不可超过100！';
			}
		},
		/*jumpurl: [/[^\s]{1,120}$/,"请填写图片正确的链接地址！"],*/
		//jumpurl: [/^((http){1}:\/\/)[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$/,"请填写长度不大于120的合法链接地址！"],
		comment:function (value){
			if(value.trim().length >2000){
				return '项目领域备注长度不可超过2000！';
			}
		}
	});

	
	//监听提交
	form.on('submit(addprojectarea)', function(data){
		var msg = addprojectfields(data.field);
		if(msg==1){
			layer.alert("添加失败！",{
				skin: 'layui-layer-red' //样式类名
					,closeBtn: 0
			});
			return false;
		}else if(msg==0){
			layer.alert('添加成功！',{
				skin: 'layui-layer-red' //样式类名
					,closeBtn: 0
			},function(){
				parent.layer.closeAll();
			});
			return false;
		}else if(msg==2){
			layer.alert("系统错误！请联系管理员！",{
				skin: 'layui-layer-red' //样式类名
					,closeBtn: 0
			});
			return false;
		}
	});


});

function cancleprojectarea(){
	parent.layer.closeAll();
}

function addprojectfields(val){
	$.ajax({
		type:'post',
		url:contextPath+'/sys/sysprojectfields/addparea',
		async:false,
		data:val,
		dataType:'json',
		success:function(res){
			if(res.success){
				count = 0;
			}else{
				count = 1;
			}
		},
		error:function(){
			count = 2;
		}
	})
	return count;
}

function checknone(val){
	var count = 1;
	$.ajax({
		type:'post',
		url:contextPath+'/sys/sysprojectfields/getcount?search='+val,
		async:false,
		dataType:'json',
		success:function(res){
			if(res.success){
				count = 0;
			}else{
				count = 1;
			}
		},
		error:function(){
			count = 2;
		}
	})
	return count==1?"不可使用":"";
}



